<?php
include_once("config.php");

session_start();
if (isset($_COOKIE['user']))
{
	$userID=$_COOKIE['user'];
	if ($userID > 0)
	{
		$repo = new UserRepo();
		$user = $repo->get($userID);
		getInfo($user);
	}

}
else
{
	$userName=$_POST['userName'];
	$password=$_POST['password'];
	$repo = new UserRepo();
	$user = $repo->findUser($userName, $password);
	if ($user[0])
	{
		getInfo($user[0]);
	}
	else
	{
		echo "LOGIN FAILED";
		//header("location:login.php");
	}
}

function getInfo($user)
{
	$_SESSION["user"] = $user;
	$repo = new TeamRepo();
	$teams = $repo->getAllTeamsForUser($user->id);
	if (isset($teams))
	{
		if (count($teams) > 1)
		{
			echo "TODO: account for someone having multiple teams";	
		}
		$_SESSION['team'] = $teams[0];
		header("location:setcookie.php");
	}
	else
	{
		header("location:chooseteam.php");
	}
}
?>